package com.wenhui.schoolyardinfo.square.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.QMUITopBar;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.base.BaseActivity;
import com.wenhui.schoolyardinfo.firstpage.ui.SearchActivity;
import com.wenhui.schoolyardinfo.square.adapter.FmViewPagerAdapter;
import com.wenhui.schoolyardinfo.square.fragment.FmFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author    : mahongiyn
 * e-mail    : 1976222027@qq.com
 * date      : 2019/7/16 23:45
 * introduce :
 */

public class FmActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.viewpager_fm)
    ViewPager fmViewpager;
    @BindView(R.id.imv_back)
    ImageView imvBack;
    @BindView(R.id.layout_back)
    RelativeLayout layoutBack;
    @BindView(R.id.imv_search)
    ImageView imvSearch;
    @BindView(R.id.layout_search)
    RelativeLayout layoutSearch;
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.img1)
    ImageView img1;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.ly1)
    LinearLayout ly1;
    @BindView(R.id.img2)
    ImageView img2;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.ly2)
    LinearLayout ly2;
    @BindView(R.id.img3)
    ImageView img3;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.ly3)
    LinearLayout ly3;
    @BindView(R.id.img4)
    ImageView img4;
    @BindView(R.id.tv4)
    TextView tv4;
    @BindView(R.id.ly4)
    LinearLayout ly4;
    @BindView(R.id.img5)
    ImageView img5;
    @BindView(R.id.tv5)
    TextView tv5;
    @BindView(R.id.ly5)
    LinearLayout ly5;
    private ViewPager viewPager;
    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fm);
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {



    }


    @Override
    protected void initViews() {
        fragments = new ArrayList<>();
        fragments.add(FmFragment.newInstance());
        fragments.add(FmFragment.newInstance());
        fragments.add(FmFragment.newInstance());
        fragments.add(FmFragment.newInstance());
        fragments.add(FmFragment.newInstance());

        FmViewPagerAdapter adapter = new FmViewPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(this);

        // 默认布局;
        tv1.setTextColor(ContextCompat.getColor(this, R.color.white));
        img1.setImageResource(R.mipmap.tab_hot);
//        ly1.setBackgroundResource(R.color.bar_grey);
        viewPager.setCurrentItem(0);
        //  getSupportFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit();

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setView();
        switch (position) {
            case 0:
               
                //ly1.setBackgroundResource(R.color.bar_grey);
                img1.setImageResource(R.mipmap.tab_entertainment);
                tv1.setTextColor(ContextCompat.getColor(this, R.color.white));
                viewPager.setCurrentItem(0);
                break;

            case 1:
                
                //ly2.setBackgroundResource(R.color.bar_grey);
                img2.setImageResource(R.mipmap.tab_hot);
                tv2.setTextColor(ContextCompat.getColor(this, R.color.bar_grey_90));
                viewPager.setCurrentItem(1);
                break;
            case 2:
               // ly3.setBackgroundResource(R.color.bar_grey);
               img3.setImageResource(R.mipmap.tab_hot);
                tv3.setTextColor(ContextCompat.getColor(this, R.color.bar_grey_90));
                viewPager.setCurrentItem(2);
                break;
            case 3:
               // ly4.setBackgroundResource(R.color.bar_grey);
                img4.setImageResource(R.mipmap.tab_hot);
                tv4.setTextColor(ContextCompat.getColor(this, R.color.bar_grey_90));
                viewPager.setCurrentItem(3);
                break;
                case 4:
               // ly5.setBackgroundResource(R.color.bar_grey);
                img5.setImageResource(R.mipmap.tab_hot);
                tv5.setTextColor(ContextCompat.getColor(this, R.color.bar_grey_90));
                viewPager.setCurrentItem(3);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    private void setView() {
        //每点击按钮之前恢复默认
        tv1.setTextColor(ContextCompat.getColor(this, R.color.bar_grey));
        tv2.setTextColor(ContextCompat.getColor(this, R.color.bar_grey));
        tv3.setTextColor(ContextCompat.getColor(this, R.color.bar_grey));
        tv4.setTextColor(ContextCompat.getColor(this, R.color.bar_grey));
        ly1.setBackgroundResource(R.color.bar_grey);
        ly2.setBackgroundResource(R.color.bar_grey);
        ly3.setBackgroundResource(R.color.bar_grey);
        ly4.setBackgroundResource(R.color.bar_grey);
        img1.setImageResource(R.mipmap.tab_hot);
        img2.setImageResource(R.mipmap.tab_hot);
        img3.setImageResource(R.mipmap.tab_hot);
        img4.setImageResource(R.mipmap.tab_hot);
    }
    @OnClick({R.id.layout_back, R.id.layout_search, R.id.ly1, R.id.ly2, R.id.ly3, R.id.ly4, R.id.ly5})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.layout_search:
                startActivity(SearchActivity.class);
                break;
            case R.id.ly1:
               // ly1.setBackgroundResource(R.color.bar_grey);
                img1.setImageResource(R.mipmap.tab_hot);
                tv1.setTextColor(ContextCompat.getColor(this, R.color.white));
                viewPager.setCurrentItem(0);
                break;
            case R.id.ly2:
                img2.setImageResource(R.mipmap.tab_hot);
                tv2.setTextColor(ContextCompat.getColor(this, R.color.white));
                viewPager.setCurrentItem(1);
                break;
            case R.id.ly3:
                img3.setImageResource(R.mipmap.tab_hot);
                tv3.setTextColor(ContextCompat.getColor(this, R.color.white));
                viewPager.setCurrentItem(2);
                break;
            case R.id.ly4:
                img4.setImageResource(R.mipmap.tab_hot);
                tv4.setTextColor(ContextCompat.getColor(this, R.color.white));
                viewPager.setCurrentItem(3);
                break;
            case R.id.ly5:
                img5.setImageResource(R.mipmap.tab_hot);
                tv5.setTextColor(ContextCompat.getColor(this, R.color.white));
                viewPager.setCurrentItem(4);
                break;
        }
    }
}
